package com.student.manager.servlet;

import com.student.manager.bean.Student;
import com.student.manager.service.StudentService;
import com.student.manager.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        try {
            int sid = Integer.parseInt(request.getParameter("sid"));
            String sname = request.getParameter("sname"); //sname:zhangsan
            String gender = request.getParameter("gender");
            String phone = request.getParameter("phone");
            String birthday = request.getParameter("birthday"); // 1989-10-18
            String info = request.getParameter("info");
            //String hobby = request.getParameter("hobby");//hobby : 游泳，写字， 足球。
            String[] h = request.getParameterValues("hobby");
            //		[篮球，足球，写字] --- 篮球，足球，写字
            String hobby = Arrays.toString(h);
            hobby = hobby.substring(1, hobby.length() - 1);

            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
            Student student = new Student(sname,gender,phone,hobby,info,date);
            student.setSid(sid);

            System.out.println("需要更新的学生信息:" + student);
            StudentService service = new StudentServiceImpl();
            service.update(student);
            request.getRequestDispatcher("StudentListServlet").forward(request,response);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
